package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.service.AdminService;
import com.atguigu.crowd.service.AuthService;
import com.atguigu.crowd.service.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.atguigu.entity.Auth;
import com.atguigu.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class AssignController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    @ResponseBody
    @RequestMapping("/assign/do/role/assign/auth")
    public ResultEntity<String> saveRoleAuthRelathinship(@RequestBody Map<String,List<Integer>> map){
        authService.saveRoleAuthRelathinship(map);
        return ResultEntity.successData();
    }

    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth/id/by/role/id")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(Integer roleId){
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successResuleData(authIdList);
    }
    @ResponseBody
    @RequestMapping("/assign/get/all/auth")
    public ResultEntity<List<Auth>> getAllAuth(){
        List<Auth> authList = authService.getAll();
        return ResultEntity.successResuleData(authList);
    }

    @RequestMapping("/assign/do/role/assign")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam(value = "pageNum",required = false) Integer pageNum,
            @RequestParam(value = "keyWord",required = false) String keyWord,
            @RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList
    ){
        adminService.saveAdminRoleRelationship(adminId,roleIdList);

        return "redirect:/admin/get/page?pageNum="+pageNum+"&keyWord="+keyWord;
    }

    @RequestMapping("/assign/to/assign/role/page")
    public String toAssignRolePage(@RequestParam("adminId") Integer adminId, Model model){
        // 查询已分配的角色
        List<Role> assignRoleList = roleService.getAssignedRole(adminId);

        // 查询未分配的角色
        List<Role> unAssignRoleList = roleService.getUnAssignedRole(adminId);
        // 存入模型
        model.addAttribute("assignRoleList",assignRoleList);
        model.addAttribute("unAssignRoleList",unAssignRoleList);
        return "assign-role";
    }
}
